package models
{
	import mx.collections.ArrayCollection;
	
	public class Project extends Resource
	{	
		public function Project(name:String = "", ownerId:int = 0) {
			super(new XML('<project />'));
			this.name = name;
			this.ownerId = ownerId;
		}
		
		public function get oid() : int {
			return _node.@oid;
		}
		public function set oid(oid:int) : void {
			_node.@oid = oid;
		}
		
		public function get name() : String {
			return _node.@name;
		}
		public function set name(name:String) : void {
			_node.@name = name;
		}
		
		public function get owner() : User {
			var owner:User = new User();
			var xml:XML = new XML(_node.owner.toXMLString());
			xml.setName('user');
			owner.setXML(xml);
			return owner;
		}
		public function set owner(owner:User) : void {
			_node.owner = owner.toXML(false);
		}
		
		public function get ownerId() : int {
			return _node.@ownerId;
		}
		public function set ownerId(ownerId:int) : void {
			_node.@ownerId = ownerId;
		}
		
		public function get diagrams() : ArrayCollection {
			var diagrams:ArrayCollection = new ArrayCollection();
			if(typeof _node.diagrams != 'undefined') {
				var d:Diagram;
				for each(var xml:* in _node.diagrams.*) {
					d = new Diagram();
					d.setXML(xml);
					diagrams.addItem(d);
				}
			}
			return diagrams;
		}
		public function set diagrams(diagrams:ArrayCollection) : void {
			if(diagrams.length) {
				_node.diagrams = new XML('<diagrams />');
				for each(var d:Diagram in diagrams) {
					_node.diagrams.appendChild(d.toXML());
				}
			} else {
				delete _node.diagrams;
			}
		}
		
		public function get contributors() : ArrayCollection {
			var contributors:ArrayCollection = new ArrayCollection();
			if(typeof _node.contributors != 'undefined') {
				var u:User;
				for each(var xml:* in _node.contributors.*) {
					u = new User();
					u.setXML(xml);
					contributors.addItem(u);
				}
			}
			return contributors;
		}
		public function set contributors(contributors:ArrayCollection) : void {
			if(contributors.length) {
				_node.contributors = new XML('<contributors />');
				for each(var u:User in contributors) {
					_node.contributors.appendChild(u.toXML());
				}
			} else {
				delete _node.contributors;
			}
		}
	}
}